﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using MusicRipperDataAccess;

namespace UWSP_Music_Ripper
{
    class FileHelper
    {
        public FileHelper()
        {

        }

        public void MoveFiles(string inRippedDirName, string inDestinationDir, Album inAlbum)
        {
            List<string> RippedFiles = GetTracksRipped(inRippedDirName, inAlbum.Artist, inAlbum.Title);
           
            if (RippedFiles.Count != inAlbum.MyTracks.Count())
            {
                throw new Exception("mis matched number of files");
            }
            for (int index = 0; index < RippedFiles.Count; index++)
            {
                string file = Path.Combine(inDestinationDir, inAlbum.MyTracks[index].NewTitle);
                if (File.Exists(file)) File.Delete(file);
                File.Move(RippedFiles[index], file);
            }

            string directoryToDelete = Path.Combine(inRippedDirName, inAlbum.Artist);
            Directory.Delete(directoryToDelete, true);

        }
        public List<string>GetTracksRipped(string inDirName, string inArtistName, string inAlbumName)
        {
            string directoryToSearch = Path.Combine(inDirName, inArtistName, inAlbumName);
            List<string> files = Directory.GetFiles(directoryToSearch,"*.wav").ToList();
            return files;
        }
        
        private List<string> GetRippedTracksFileNames(List<string> inRippedTracks)
        {
            List<string> temp = new List<string>();
            foreach (string s in inRippedTracks)
            {
                temp.Add(Path.GetFileNameWithoutExtension(s));
            }
            return temp;
        }
    }
}
